package GUI;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Window;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.table.AbstractTableModel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import Entity.DisabledPlace;
import Entity.DisabledReport;
import javax.swing.JButton;

/**
 * this is the disabled report create GUI. the parking lot manager can chose from the table which data he need for the report and the company manager will watch it.
 * @author Shahar Shelly
 *
 */
public class DisabledReportCreateWin extends JFrame {

	private JPanel contentPane;
	private JTextField startDate;
	private JTextField endDate;
	private JTextField ComplaintNum;
	private JTextField Depth;
	private JTextField Height;
	private JTextField Column;
	private JTable table;

	/**
	 * is the constructor. it has a table listener for pouring the data and buttons for create and canel. 
	 */
	public DisabledReportCreateWin(DisabledReport r) {
		final DisabledReport c2=r;
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 760, 326);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.setLayout(null);
		
		final JLabel lblDisabledReport = new JLabel("Disabled Places Report ");
		lblDisabledReport.setBounds(175, 11, 161, 14);
		contentPane.add(lblDisabledReport);
		
		JPanel panel = new JPanel();
		panel.setBounds(10, 33, 98, 43);
		panel.setBorder(new TitledBorder(null, "Start Date", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		contentPane.add(panel);
		panel.setLayout(null);
		
		startDate = new JTextField();
		startDate.setBounds(6, 16, 86, 20);
		panel.add(startDate);
		startDate.setColumns(10);
		startDate.setText(r.getStart().getDate()+"/"+(r.getStart().getMonth()+1)+"/"+(r.getStart().getYear()+1900));
		
		JPanel panel_1 = new JPanel();
		panel_1.setBounds(10, 77, 98, 43);
		panel_1.setBorder(new TitledBorder(null, "End Date", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		contentPane.add(panel_1);
		panel_1.setLayout(null);
		
		endDate = new JTextField();
		endDate.setBounds(6, 16, 86, 20);
		panel_1.add(endDate);
		endDate.setColumns(10);
		endDate.setText(r.getEndDate().getDate()+"/"+(r.getEndDate().getMonth()+1)+"/"+(r.getEndDate().getYear()+1900));
		
		JPanel panel_2 = new JPanel();
		panel_2.setBounds(10, 131, 180, 108);
		panel_2.setBorder(new TitledBorder(null, "Description", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		contentPane.add(panel_2);
		panel_2.setLayout(null);
		
		final JTextPane textPane = new JTextPane();
		textPane.setBounds(6, 16, 164, 81);
		panel_2.add(textPane);
		
		JLabel lblNumberOfComplaints = new JLabel("Number of Disabled Spaces:");
		lblNumberOfComplaints.setBounds(252, 255, 151, 14);
		contentPane.add(lblNumberOfComplaints);
		
		ComplaintNum = new JTextField();
		ComplaintNum.setBounds(413, 252, 40, 20);
		contentPane.add(ComplaintNum);
		ComplaintNum.setColumns(10);
		ComplaintNum.setText(String.valueOf(r.getDisabledNumber()));
		
		JLabel lblDepth = new JLabel("Depth:");
		lblDepth.setBounds(572, 33, 81, 14);
		contentPane.add(lblDepth);
		
		Depth = new JTextField();
		Depth.setBounds(617, 30, 86, 20);
		contentPane.add(Depth);
		Depth.setColumns(10);
		
		JPanel panel_3 = new JPanel();
		panel_3.setBorder(new TitledBorder(null, "Description:", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_3.setBounds(572, 117, 168, 92);
		contentPane.add(panel_3);
		panel_3.setLayout(null);
		
		final JTextPane textPane_1 = new JTextPane();
		textPane_1.setBounds(6, 16, 150, 69);
		panel_3.add(textPane_1);
		
		JLabel label = new JLabel("Height:");
		label.setBounds(572, 59, 81, 14);
		contentPane.add(label);
		
		Height = new JTextField();
		Height.setColumns(10);
		Height.setBounds(617, 56, 86, 20);
		contentPane.add(Height);
		
		JLabel label_1 = new JLabel("Column:");
		label_1.setBounds(572, 89, 81, 14);
		contentPane.add(label_1);
		
		Column = new JTextField();
		Column.setColumns(10);
		Column.setBounds(617, 86, 86, 20);
		contentPane.add(Column);
		
		table = new JTable(new MyDisabled(r.getDisable()));
		table.setBounds(270, 64, 241, 124);
		JScrollPane sp=new JScrollPane(table);
		sp.setBounds(200, 36, 352, 203);
		contentPane.add(sp);
		
		JButton btnSend = new JButton("Send");
		btnSend.setBounds(10, 250, 89, 23);
		contentPane.add(btnSend);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.setBounds(115, 251, 89, 23);
		contentPane.add(btnCancel);
		
		table.addMouseListener(new MouseAdapter() {
			  public void mouseClicked(MouseEvent e) {
			    if (e.getClickCount() == 2) {
			      JTable target = (JTable)e.getSource();
			      int row = target.getSelectedRow();
			      Depth.setText(String.valueOf(c2.getDisable()[row].getPlace()[0]));
			      Column.setText(String.valueOf(c2.getDisable()[row].getPlace()[1]));
			      Height.setText(String.valueOf(c2.getDisable()[row].getPlace()[2]));
			      textPane.setText(c2.getDisable()[row].getDescription());
			    }
			  }
		});
	}

}

class MyDisabled extends AbstractTableModel
{
	String[] titles={"Disabled Number","Start Date","End Date"};
	Object[][] data;
	
	public MyDisabled(DisabledPlace[] c)
	{
		super();
		this.data=new Object[c.length][3];
		for(int i=0; i<c.length; i++)
		{
			this.data[i][0]=c[i].getPid();
			this.data[i][1]=c[i].getStart();
			if(c[i].getEnd()!=null)
				this.data[i][2]=c[i].getEnd();
			else
				this.data[i][2]="Still Disable";
		}
	}
	@Override
	public int getColumnCount() {
		return this.titles.length;
	}

	@Override
	public int getRowCount() {
		return data.length;
	}

	@Override
	public Object getValueAt(int arg0, int arg1) {
		return data[arg0][arg1];
	}
	
	@Override
	public String getColumnName(int column) {
		// TODO Auto-generated method stub
		return (String) this.titles[column];
	}
}

